Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The thunky npm package is designed to delay a function's execution until it's actually needed. It wraps a function in a way that ensures it is only called once, regardless of how many times the wrapper is invoked. This is particularly useful for initialization functions that you don't want to run multiple times or expensive operations that should be deferred until absolutely necessary.
Lazy initialization
This feature allows for the lazy initialization of resources. The provided code sample demonstrates how a potentially expensive operation (simulated with a setTimeout) is wrapped in a thunky function. Despite calling 'load' twice, the initialization code only runs once, and both calls receive the result once it's available.
const thunky = require('thunky');
let load = thunky((callback) => {
console.log('Initializing...');
setTimeout(() => callback(null, 'Initialization complete'), 1000);
});
load((err, message) => console.log(message));
load((err, message) => console.log(message));
The 'once' package ensures a function can only be called once. It's similar to thunky in preventing multiple invocations, but it doesn't support lazy initialization. Instead, it's focused on ensuring a function doesn't execute more than once, regardless of its purpose.
Memoizee is a comprehensive memoization library that caches the results of function calls based on their arguments. While it serves a different primary purpose than thunky, it achieves a similar effect of avoiding repeated expensive operations. However, memoizee is more versatile in caching multiple calls with different arguments, whereas thunky is focused on single or no-argument functions that need to be delayed or ensured to only run once.
Delay the evaluation of a paramless async function and cache the result (see thunk).
npm install thunky
Let's make a simple function that returns a random number 1 second after it is called for the first time
var thunky = require('thunky')
var test = thunky(function (callback) { // the inner function should only accept a callback
console.log('waiting 1s and returning random number')
setTimeout(function () {
callback(Math.random())
}, 1000)
})
test(function (num) { // inner function is called the first time we call test
console.log(num) // prints random number
})
test(function (num) { // subsequent calls waits for the first call to finish and return the same value
console.log(num) // prints the same random number as above
})
Thunky makes it easy to implement a lazy evaluation pattern.
var getDb = thunky(function (callback) {
db.open(myConnectionString, callback)
})
var queryDb = function (query, callback) {
getDb(function (err, db) {
if (err) return callback(err)
db.query(query, callback)
})
}
queryDb('some query', function (err, result) { ... } )
queryDb('some other query', function (err, result) { ... } )
The first time getDb
is called it will try do open a connection to the database.
Any subsequent calls will just wait for the first call to complete and then call your callback.
A nice property of this pattern is that it easily allows us to pass any error caused by getDb
to the queryDb
callback.
If the thunk callback is called with an Error
object as the first argument it will not cache the result
var fails = thunky(function (callback) {
console.log('returning an error')
callback(new Error('bad stuff'))
})
fails(function (err) { // inner function is called
console.log(err)
});
fails(function (err) { // inner function is called again as it returned an error before
console.log(err)
})
A promise version is available as well
var thunkyp = require('thunky/promise')
var ready = thunkyp(async function () {
// ... do async stuff
return 42
})
// same semantics as the callback version
await ready()
MIT
FAQs
delay the evaluation of a paramless async function and cache the result
The npm package thunky receives a total of 7,382,713 weekly downloads. As such, thunky popularity was classified as popular.
We found that thunky demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.